home *** CD-ROM | disk | FTP | other *** search
- Path: news.uiowa.edu!usenet
- From: Greg Hergert <gregory-hergert@uiowa.edu>
- Newsgroups: comp.lang.c++
- Subject: Dynamic allocation of 2D Array Question
- Date: 5 Mar 1996 20:09:12 GMT
- Organization: University of Iowa
- Distribution: world
- Message-ID: <4hi718$19hk@flood.weeg.uiowa.edu>
- NNTP-Posting-Host: mcm14.itc.uiowa.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Macintosh; I; PPC)
- X-URL: news:comp.lang.c++
-
- Hello, I have a question about declairing a two dimensional
- array (matrix) dynamically. I would like to have a matrix
- that can be created dynamically (not statically).
-
- My code here is:
- <<< code.h file >>
-
- class grid
- {
- public:
- grid (int size); // constructor
- ~grid () {} // destructor
-
- private:
- int SizeofGrid;
- enum status { occupied, unoccupied };
- enum map { visited, unvisited };
-
- struct square
- {
- status S;
- map M;
- int tag;
- };
-
- square* rep [SizeofGrid] [SizeofGrid];
- };
-
-
- << code.cpp file // implementation... >>
-
- #include "grid.h"
-
- //definition of constructor
-
- grid::grid (int size)
- {
- rep = new [size] [size]; //create the matrix
- SizeofGrid = size; // set private member size
- }
-
- in my client program:::
-
- void main ()
- {
- int N;
- cout << "enter size of grid" ;
- cin >> N;
- grid testgrid(N);
- }
-
-
- The way that I am solving this problem is by declairing my
- matrix as a static array instead of dynamically:
- i.e.
-
- header file:::
- ..
- private:
- square rep [10][10];
- ..
-
- Is there a way around this, or somewhere where my syntax is
- incorrect? I've tried the code above on Codewarrior, g++
- and BC++ 4.5, they all give errors about the declaration
- of the array as a 'new'....
-
- Thanks for your help! In addition to posting suggestions
- to the newsgroup, could you also e-mail me a copy! :)
-
- gregory-hergert@uiowa.edu
-
-
-
-
-